package xyz.longicorn.driver.config;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import xyz.longicorn.driver.dto.ApiResult;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
public class ResponseExceptionConfig {

    @ExceptionHandler(value = BizException.class) // 要捕获的异常类型
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public ApiResult bizExceptionHandler(HttpServletRequest req, BizException e) {
        return ApiResult.error(e.getCode(), e.getMessage());
    }

    // 没有单独处理的异常都会被此方法处理
    @ExceptionHandler(value = Exception.class) // 要捕获的异常类型
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public ApiResult exceptionHandler(HttpServletRequest req, Exception e) {
        return ApiResult.error(-1, "App Internal Error:" + e.getMessage());
    }
}
